import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  {
    path: '/login',
    name: 'Login',
    component:  () => import(/* webpackChunkName: "login" */ '../views/login.vue')
  },
  {
    path: '/user',
    name: 'User',
    // route level code-splitting
    // this generates a separate chunk (about.[hash].js) for this route
    // which is lazy-loaded when the route is visited.
    component: () => import(/* webpackChunkName: "user" */ '../views/user.vue')
  }
]


const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})

router.beforeEach((to,from,next) => {
    console.log(to)
    // let verifyArr = ['/user'];
    if(to.path === '/user'){
        let token = localStorage.getItem('token') || '';
        if(token){
            next();
        }else{
            next('/login')
        }
    }else{
        next()
    }
})

export default router
